package com.chixing.controller;

import com.chixing.entity.OrderDetail;
import com.chixing.entity.vo.OrderQueryVO;
import com.chixing.service.IOrderDetailService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 订单详情前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-10-01
 */
@RestController
@RequestMapping("/order-detail")
public class OrderDetailController {

    @Autowired
    private IOrderDetailService orderDetailService;

    // 查询所有订单详情
    @GetMapping
    public List<OrderDetail> getAllOrderDetails() {
        return orderDetailService.getAllOrderDetails();
    }

    // 根据ID查询订单详情
    @GetMapping("/{id}")
    public OrderDetail getOrderDetailById(@PathVariable Integer id) {
        return orderDetailService.getOrderDetailById(id);
    }

    // 根据订单ID查询详情
    @GetMapping("/order/{orderId}")
    public List<OrderDetail> getDetailsByOrderId(@PathVariable Integer orderId) {
        return orderDetailService.getDetailsByOrderId(orderId);
    }

    // 新增订单详情
    @PostMapping
    public boolean saveOrderDetail(@RequestBody OrderDetail orderDetail) {
        return orderDetailService.saveOrderDetail(orderDetail);
    }

    // 更新订单详情
    @PutMapping
    public boolean updateOrderDetail(@RequestBody OrderDetail orderDetail) {
        return orderDetailService.updateOrderDetail(orderDetail);
    }

    // 删除订单详情
    @DeleteMapping("/{id}")
    public boolean removeOrderDetail(@PathVariable Integer id) {
        return orderDetailService.removeOrderDetailById(id);
    }



    /**
     * 根据用户ID查询订单
     */
    @GetMapping("/user/{userId}")
    public Result getOrdersByUserId(@PathVariable Integer userId) {
        if (userId == null) {
            return Result.getFail("用户ID不能为空");
        }

        List<OrderQueryVO> orders = orderDetailService.getOrdersByUserId(userId);
        return Result.getSuccess(orders);
    }
    /**
     * 根据用户ID和订单状态查询订单
     * @param userId 用户ID
     * @param status 订单状态（可为null，null时查全部）
     */
    @GetMapping("/user/{userId}/status/{status}")
    public Result getOrdersByUserIdAndStatus(
            @PathVariable Integer userId,
            @PathVariable(required = false) Integer status) { // 设置为非必填
        if (userId == null) {
            return Result.getFail("用户ID不能为空");
        }
        List<OrderQueryVO> orders = orderDetailService.getOrdersByUserIdAndStatus(userId, status);
        return Result.getSuccess(orders);
    }
    /**
     * 根据订单号查询订单
     */
    @GetMapping("/number/{orderNumber}")
    public Result getOrderByNumber(@PathVariable String orderNumber) {
        if (orderNumber == null || orderNumber.isEmpty()) {
            return Result.getFail("订单号不能为空");
        }

        OrderQueryVO order = orderDetailService.getOrderByNumber(orderNumber);
        if (order == null) {
            return Result.getFail("未找到该订单");
        }

        return Result.getSuccess(order);
    }




}
